package com.yang.jpa.service;

import com.yang.jpa.dao.*;
import com.yang.jpa.model.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.criteria.*;
import java.util.*;

@Service
public class AttendanceSheetService {

    @Autowired
    private CheckAttendanceSheetDao checkAttendanceSheetDao;

    @Autowired
    private StaffDao staffDao;

    @Autowired
    private AttendanceSheetDao attendanceSheetDao;

    @Autowired
    private RoleDao roleDao;

    @Autowired
    private StaffRoleDao staffRoleDao;

    @Transactional
    public Map<String, Object> toExamine(Integer checkAttendanceSheetId){

        Specification<CheckAttendanceSheet> checkSpecification = new Specification<CheckAttendanceSheet>() {
            @Override
            public Predicate toPredicate(Root<CheckAttendanceSheet> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                Path<Object> id = root.get("id");
                Predicate predicateId = criteriaBuilder.equal(id, checkAttendanceSheetId);

                return criteriaBuilder.and(predicateId);
            }
        };
        CheckAttendanceSheet check = checkAttendanceSheetDao.findOne(checkSpecification).get();

        Staff staff = staffDao.findStaff(check.getId());
        System.out.println("staff："+staff);

        Specification<AttendanceSheet> sheetSpecification = new Specification<AttendanceSheet>() {
            @Override
            public Predicate toPredicate(Root<AttendanceSheet> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                Path<Object> id = root.get("id");
                Predicate predicateId = criteriaBuilder.equal(id, check.getAttendanceSheetId());

                return criteriaBuilder.and(predicateId);
            }
        };
        AttendanceSheet attendanceSheet = attendanceSheetDao.findOne(sheetSpecification).get();
        System.out.println("attendanceSheet："+attendanceSheet);

        if (check.getAttendanceSheetState() == 1){

            Specification<StaffRole> srSpecification = new Specification<StaffRole>() {
                @Override
                public Predicate toPredicate(Root<StaffRole> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                    Path<Object> staffId = root.get("staffId");
                    Predicate predicateStaffId = criteriaBuilder.equal(staffId, staff.getId());
                    Path<Object> roleId = root.get("roleId");
                    Predicate predicateRoleId = criteriaBuilder.equal(roleId, 1);

                    return criteriaBuilder.and(predicateStaffId, predicateRoleId);
                }
            };

            StaffRole staffRole = null;
            if (!staffRoleDao.findOne(srSpecification).isEmpty()){
                staffRole = staffRoleDao.findOne(srSpecification).get();
                System.out.println("staffRole："+staffRole);
            }

            Specification<StaffRole> srSpecification1 = new Specification<StaffRole>() {
                @Override
                public Predicate toPredicate(Root<StaffRole> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                    Path<Object> staffId = root.get("staffId");
                    Predicate predicateStaffId = criteriaBuilder.equal(staffId, staff.getId());
                    Path<Object> roleId = root.get("roleId");
                    Predicate predicateRoleId = criteriaBuilder.equal(roleId, 5);

                    return criteriaBuilder.and(predicateStaffId, predicateRoleId);
                }
            };

            StaffRole staffRole1 = null;
            if (!staffRoleDao.findOne(srSpecification1).isEmpty()) {
                staffRole1 = staffRoleDao.findOne(srSpecification1).get();
                System.out.println("staffRole1：" + staffRole1);
            }

            if (staffRole != null){
                if (attendanceSheet.getDuration() >= 72){
                    CheckAttendanceSheet checkAttendanceSheet = new CheckAttendanceSheet();
                    checkAttendanceSheet.setAttendanceSheetId(check.getAttendanceSheetId());
                    checkAttendanceSheet.setAttendanceSheetState(2);
                    checkAttendanceSheet.setPass(1);
                    checkAttendanceSheetDao.save(checkAttendanceSheet);

                    check.setPass(2);
                    checkAttendanceSheetDao.save(check);

                    attendanceSheet.setState(2);
                    attendanceSheetDao.save(attendanceSheet);
                }else {
                    CheckAttendanceSheet checkAttendanceSheet = new CheckAttendanceSheet();
                    checkAttendanceSheet.setAttendanceSheetId(check.getAttendanceSheetId());
                    checkAttendanceSheet.setAttendanceSheetState(3);
                    checkAttendanceSheet.setPass(1);
                    checkAttendanceSheetDao.save(checkAttendanceSheet);

                    check.setPass(2);
                    checkAttendanceSheetDao.save(check);

                    attendanceSheet.setState(3);
                    attendanceSheetDao.save(attendanceSheet);
                }
            }else if (staffRole1 != null){
                if (attendanceSheet.getDuration() >= 168){
                    CheckAttendanceSheet checkAttendanceSheet = new CheckAttendanceSheet();
                    checkAttendanceSheet.setAttendanceSheetId(check.getAttendanceSheetId());
                    checkAttendanceSheet.setAttendanceSheetState(2);
                    checkAttendanceSheet.setPass(1);
                    checkAttendanceSheetDao.save(checkAttendanceSheet);

                    check.setPass(2);
                    checkAttendanceSheetDao.save(check);

                    attendanceSheet.setState(2);
                    attendanceSheetDao.save(attendanceSheet);
                }else {
                    CheckAttendanceSheet checkAttendanceSheet = new CheckAttendanceSheet();
                    checkAttendanceSheet.setAttendanceSheetId(check.getAttendanceSheetId());
                    checkAttendanceSheet.setAttendanceSheetState(3);
                    checkAttendanceSheet.setPass(1);
                    checkAttendanceSheetDao.save(checkAttendanceSheet);

                    check.setPass(2);
                    checkAttendanceSheetDao.save(check);

                    attendanceSheet.setState(3);
                    attendanceSheetDao.save(attendanceSheet);
                }
            }
        }else if (check.getAttendanceSheetState() == 2){
            CheckAttendanceSheet checkAttendanceSheet = new CheckAttendanceSheet();
            checkAttendanceSheet.setAttendanceSheetId(check.getAttendanceSheetId());
            checkAttendanceSheet.setAttendanceSheetState(3);
            checkAttendanceSheet.setPass(1);
            checkAttendanceSheetDao.save(checkAttendanceSheet);

            check.setPass(2);
            checkAttendanceSheetDao.save(check);

            attendanceSheet.setState(3);
            attendanceSheetDao.save(attendanceSheet);
        }else if (check.getAttendanceSheetState() == 3){
            CheckAttendanceSheet checkAttendanceSheet = new CheckAttendanceSheet();
            checkAttendanceSheet.setAttendanceSheetId(check.getAttendanceSheetId());
            checkAttendanceSheet.setAttendanceSheetState(4);
            checkAttendanceSheet.setPass(1);
            checkAttendanceSheetDao.save(checkAttendanceSheet);

            check.setPass(2);
            checkAttendanceSheetDao.save(check);

            attendanceSheet.setState(4);
            attendanceSheetDao.save(attendanceSheet);
        }else if (check.getAttendanceSheetState() == 4){

            check.setPass(2);
            checkAttendanceSheetDao.save(check);

            attendanceSheet.setState(5);
            attendanceSheetDao.save(attendanceSheet);
        }

        Map<String, Object> msg = new HashMap<String, Object>();
        msg.put("massage", "审核成功");
        msg.put("code", 200);
        return msg;
    }

    public List<CheckAttendanceSheet> findCheckAttendanceSheet(Integer staffId){

        Specification<Staff> staffSpecification = new Specification<Staff>() {
            @Override
            public Predicate toPredicate(Root<Staff> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                Path<Object> id = root.get("id");
                Predicate predicateId = criteriaBuilder.equal(id, staffId);

                return criteriaBuilder.and(predicateId);
            }
        };
        Staff staff = staffDao.findOne(staffSpecification).get();
        System.out.println("staff："+staff);

        List<Role> roleList = roleDao.findStaffRole(staff.getId());
        System.out.println("roleList："+roleList);

        List<CheckAttendanceSheet> checkAttendanceSheetList = new ArrayList<>();
        for (Role role: roleList
             ) {
            if (role.getId() == 1){
                checkAttendanceSheetList.addAll(checkAttendanceSheetDao.findCheckAttendanceSheet(staff.getOrganId()+"%", 1, staff.getOrganId().length()+2));
            }else if (role.getId() == 4){
                checkAttendanceSheetList.addAll(checkAttendanceSheetDao.findCheckAttendanceSheetNew(2));
            }else if (role.getId() == 2){
                checkAttendanceSheetList.addAll(checkAttendanceSheetDao.findCheckAttendanceSheetNew(3));
            }else if (role.getId() == 3){
                checkAttendanceSheetList.addAll(checkAttendanceSheetDao.findCheckAttendanceSheetNew(4));
            }
        }

        return checkAttendanceSheetList;
    }

    @Transactional
    public Map<String, Object> passAudit(Integer checkAttendanceSheetId){

        Specification<CheckAttendanceSheet> checkSpecification = new Specification<CheckAttendanceSheet>() {
            @Override
            public Predicate toPredicate(Root<CheckAttendanceSheet> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                Path<Object> id = root.get("id");
                Predicate predicateId = criteriaBuilder.equal(id, checkAttendanceSheetId);

                return criteriaBuilder.and(predicateId);
            }
        };
        CheckAttendanceSheet check = checkAttendanceSheetDao.findOne(checkSpecification).get();

        Specification<AttendanceSheet> sheetSpecification = new Specification<AttendanceSheet>() {
            @Override
            public Predicate toPredicate(Root<AttendanceSheet> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                Path<Object> id = root.get("id");
                Predicate predicateId = criteriaBuilder.equal(id, check.getAttendanceSheetId());

                return criteriaBuilder.and(predicateId);
            }
        };
        AttendanceSheet attendanceSheet = attendanceSheetDao.findOne(sheetSpecification).get();

        attendanceSheet.setState(6);
        attendanceSheetDao.save(attendanceSheet);

        check.setPass(3);
        checkAttendanceSheetDao.save(check);

        Map<String, Object> msg = new HashMap<String, Object>();
        msg.put("massage", "退回成功");
        msg.put("code", 200);
        return msg;
    }

    @Transactional
    public Map<String, Object> submit(int type,
             Integer staffId,
             String staffName,
             String attendanceSheetDate,
             Date startTime,
             Date endTime,
             int duration){

        AttendanceSheet attendanceSheet = new AttendanceSheet();
        attendanceSheet.setType(type);
        attendanceSheet.setState(1);
        attendanceSheet.setDuration(duration);
        attendanceSheet.setAttendanceSheetDate(attendanceSheetDate);
        attendanceSheet.setStaffId(staffId);
        attendanceSheet.setStaffName(staffName);
        attendanceSheet.setStartTime(startTime);
        attendanceSheet.setEndTime(endTime);
        attendanceSheetDao.save(attendanceSheet);

        CheckAttendanceSheet checkAttendanceSheet = new CheckAttendanceSheet();
        checkAttendanceSheet.setPass(1);
        checkAttendanceSheet.setAttendanceSheetState(1);
        checkAttendanceSheet.setAttendanceSheetId(attendanceSheet.getId());
        checkAttendanceSheetDao.save(checkAttendanceSheet);

        Map<String, Object> msg = new HashMap<String, Object>();
        msg.put("massage", "提交成功");
        msg.put("code", 200);
        return msg;
    }
}
